<?php 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

	class WxstaffController extends BaseController
	{

		/*园区房间列表页*/	
		// public function index(){

		// 	$where=array();	
		// 	$wheres=array();	
		// 	$customer_type = $this->request->param('customer_type');
		// 	if($customer_type){
		// 		$where['customer_type'] = $customer_type;	
		// 	}

		// 	/**搜索条件**/
		// 	$customer_name = $this->request->param('customer_name');
		// 	$linkman = $this->request->param('linkman');
		// 	$stime = $this->request->param('start_time');
		// 	$etime = $this->request->param('end_time');


		// 	if ($customer_name) {
		// 		$where['customer_name'] = ['like', "%$customer_name%"];
		// 	}
		// 	if ($linkman) {
		// 		$where['salesman'] = ['like', "%$linkman%"];
		// 	}	
		// 	$sql='';	
		// 	if ($stime) {
		// 		$sql .= 'strtotime >= "'.strtotime($stime).'"';
		// 	}
		// 	if ($etime) {
			
		// 		$sql .= ' and strtotime <= "'.strtotime($etime).'" ';
		// 	}
		// 	if(session('incubator_id')){
		// 		$where['incubator_id']=session('incubator_id');
		// 	}
		// 	if(session('role_id')==3){
		// 		$where['sales_id']=session('ADMIN_ID');
		// 	}
		// 	//echo $sql;

		// 	$where['status']=1;
		// 	$list=Db::name('customer')->where($where)->where($wheres)->where($sql)->paginate(5);
		// 	$page=$list->render();

		// 	$this->assign("customer_type", $customer_type);
		// 	$this->assign("customer_name", $customer_name);
		// 	$this->assign("salesman", $linkman);
		// 	$this->assign("stime", $stime);
		// 	$this->assign("etime", $etime);

		// 	$this->assign("page", $page);
		// 	$this->assign("list", $list);
		// 	return $this->fetch();
		// }



		/*园区添加*/		
		public function addStaff(){
			if($_POST){
				
				$data['customer_id'] = $this->request->param('customer_id',0);
				$data['staff_name'] = $this->request->param('staff_name');
				$data['staff_phone'] = $this->request->param('staff_phone','0');
				$data['is_use'] = $this->request->param('is_use',1);
				$data['staff_type'] = $this->request->param('staff_type',2);
				$data['add_time']=time();
				
				$add=Db::name('customer')->insert($data);

				if($add){
					$this->success('添加成功','Customer/index');
				}else{
					$this->error('添加失败');
				}	
			}else{
				
				$where=array();	
				$customers=db('customer')->field('id,customer_name')->slelect();
				$this->assign('customers',$customers);
				return $this->fetch();
			}






		}
		public function addCustomers(){
			if($_POST){
				$salesman=$this->request->param('salesman',0);
				$data['customer_name'] = $this->request->param('customer_name','');
				$data['customer_type'] = $this->request->param('customer_type',0);
				$data['linkman'] = $this->request->param('linkman','');
				$data['mobile'] = $this->request->param('mobile',0);
				$data['salesmobile'] = $this->request->param('salesmobile',0);
				$data['position'] = $this->request->param('position','');
				$data['field'] = $this->request->param('field',0);
				$data['stage'] = $this->request->param('stage',0);
				$data['address'] = $this->request->param('address','');
				$data['source'] = $this->request->param('source','');
				$data['follow_state'] = $this->request->param('follow_state',0);
				$data['follow_stage'] = $this->request->param('follow_stage',0);
				$data['salesman'] = get_user_name($salesman);
				$data['sales_id'] = $salesman;

				$data['salesmobile'] = $this->request->param('salesmobile',0);
				$data['remark'] = $this->request->param('remark','');
				$data['file'] = $this->request->param('file','');
				$data['other_link'] = $this->request->param('other_link',0);
				$data['incubator_id']=session('incubator_id');
				if(!session('incubator_id')){
					$data['incubator_id']=db('user')->where('id',$salesman)->value("incubator_id");
				}
				$data['create_time']=date('Y-m-d H:i:s');
			// echo $incubator_name ;
			// if($type_id==1){
			// 	$station_	
			// }					

			// echo $type_id;
					// var_dump($data);
					// exit;
				$add=Db::name('customer')->insert($data);

				if($add){
					$this->success('添加成功','order/addOrder');
				}else{
					$this->error('添加失败');
				}	
			}else{
				$where=array();	
				if(session('incubator_id')){
					$where['incubator_id']=session('incubator_id');
				}
				$stages=get_cate_list(10);
				// var_dump($stages);
				// if(session('role_id')==3){
				// 	$where['u.id']=session('ADMIN_ID');
				// }
				if(session('role_id')==3){
					$where['u.id']=session('ADMIN_ID');
					$this->assign('role',3);
				}else{
					$this->assign('role',500);
				}
				$salesman=db('user')->alias('u')->join('roleUser r','u.id=r.user_id')->where('role_id',3)->where($where)->field('u.id,u.user_login')->select();
				//var_dump($salesman);
				$gen_stage=get_cate_list("15");
				$link_way=get_cate_list(11);
				$namess=session('name');
				$this->assign('namess',$namess);
				$this->assign('link_way',$link_way);		
				$this->assign("gen_stage",$gen_stage);
				$this->assign("stages",$stages);
				$this->assign('salesman',$salesman);
				$cate_list=get_cate_list(6);
				$this->assign('cate_list',$cate_list);
				return $this->fetch();
			}






		}
		/*详情*/
		public function detailCustomer(){
			$where=array();
			$id= $this->request->param('id',0,'intval');
			if($id){
				$where['id']= $id;
			}else{
				// dump(info_back('','id为空',false));
				// exit;
				return info_back('','id为空',false);
			}
			$detail= Db::name('customer')->where($where)->find();
			 // dump($detail);
			// echo Db::name('station')->getLastSql();
			$where=array();	
			if(session('incubator_id')){
				$where['incubator_id']=session('incubator_id');
			}
			if(session('role_id')==3){
				$where['u.id']=session('ADMIN_ID');
			}
			$salesman=db('user')->alias('u')->join('roleUser r','u.id=r.user_id')->where('role_id',3)->where($where)->field('u.id,u.user_login')->select();
				//var_dump($salesman);
			$cate_list=get_cate_list(6);
			$stages=get_cate_list(10);
			$gen_stage=get_cate_list("15");
			$link_way=get_cate_list(11);
			$this->assign('link_way',$link_way);		
			$this->assign("gen_stage",$gen_stage);
			$this->assign('stages',$stages);
			$this->assign('cate_list',$cate_list);
			$this->assign('salesman',$salesman);
			$this->assign('detail',$detail);
			return $this->fetch();
		}

		/*编辑园区*/	
		public function editCustomer(){

			if($_POST){
				// var_dump($_POST);
				// exit;
				unset($_POST['snames']);
				$_POST['sales_id']=$_POST['salesman'];
				$_POST['salesman']=get_user_name($_POST['salesman']);
				// var_dump($_POST);
				// exit;	
				$_POST['incubator_id']=session('incubator_id');
				$edit = Db::name('customer')->update($_POST);
				$this->success('编辑成功','index');	
				if($edit){
					$this->success('编辑成功','index');

				}
			}
			$where=array();
			$id= $this->request->param('id',0,'intval');
			if($id){
				$where['id']= $id;
			}else{
				return info_back('','id不能为空',false);
			}
			$detail= Db::name('customer')->where($where)->find();
			//dump($detail);
			// exit;
			$where=array();	
			if(session('incubator_id')){
				$where['incubator_id']=session('incubator_id');
			}
			// if(session('role_id')==3){
			// 	$where['u.id']=session('ADMIN_ID');
			// }
			if(session('role_id')==3){
				$where['u.id']=session('ADMIN_ID');
				$this->assign('role',3);
			}else{
				$this->assign('role',500);
			}
			$salesman=db('user')->alias('u')->join('roleUser r','u.id=r.user_id')->where('role_id',3)->where($where)->field('u.id,u.user_login')->select();
				//var_dump($salesman);
			$stages=get_cate_list(10);
			$gen_stage=get_cate_list("15");
			$link_way=get_cate_list(11);
			$namess=session('name');
			$this->assign('namess',$namess);
			$this->assign('link_way',$link_way);		
			$this->assign("gen_stage",$gen_stage);
			$this->assign('stages',$stages);
			$this->assign('salesman',$salesman);
			$this->assign('detail',$detail);
			$cate_list=get_cate_list(6);
			$this->assign('cate_list',$cate_list);
			return $this->fetch();

		}


		/*删除园区*/
		public function delCustomer(){
			$data['id']= $this->request->param('id',0,'intval');

			$data['status']=0;

			$del=Db::name('customer')->update($data);
			if($del){
				return info_back('','删除成功',true);
			}else{
				return info_back('','删除失败',false);
			}

		}	



		/*验证业务员*/

		public function check_sales(){
			$name=$this->request->param('name','');
			$sure=$this->request->param('sure','');
			if($sure){

			}
			$result=db('user')->where('user_login',$name)->find();
			if($result){
				return 1;
			}else{
				return 0;
			}

		}	




















	}